class Solution:
    def merge(self, intervals: List[List[int]]) -> List[List[int]]:
        if len(intervals) == 0:
            return []
        res = []
        ints = sorted(intervals, key=lambda x: x[0])
        i = 0
        merge_start = ints[i][0]
        merge_end = ints[i][1]
        while i != len(ints):
            s, e = ints[i]
            if s > merge_end:
                # start a new circle
                res.append([merge_start, merge_end])
                merge_start = s
                merge_end = e
                i += 1
                continue
            if e > merge_end:
                merge_end = e
            i += 1
        res.append([merge_start, merge_end])
        return res
